function zeroPad(n:number) {
  return `0${n}`.slice(-2)
}

//  * type === 1 => YYYY-MM-DD HH:mm:ss
//  * type === 2 => YYYY-MM-DD 00:00:00
//  * type === 3 => HH:mm:ss
//  * type === 4 => MM-DD
//  * type === 5 => YYYY-MM-DD HH:mm
//  * type === null => YYYY-MM-DD
function formatDate(src:any, type?:number, split = '-') {
  const dateObject = new Date(src)
  const year = dateObject.getFullYear()
  const month = zeroPad(dateObject.getMonth() + 1)
  const date = zeroPad(dateObject.getDate())
  const hours = zeroPad(dateObject.getHours())
  const minutes = zeroPad(dateObject.getMinutes())
  const seconds = zeroPad(dateObject.getSeconds())

  const formatDate = `${year}${split}${month}${split}${date}`
  const formatTime = `${hours}:${minutes}:${seconds}`

  if (type === 1) return `${formatDate} ${formatTime}`
  if (type === 2) return `${formatDate} 00:00:00`
  if (type === 3) return formatTime
  if (type === 4) return `${month}${split}${date}`
  if (type === 5) return `${formatDate} ${hours}:${minutes}`
  if (type === 6) return `${hours}:${minutes}`
  if (type === 7) return `${minutes}:${seconds}`
  if (type === 8) return `${formatDate} 23:59:59`

  return formatDate
}

export default formatDate
